<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 8/9/2016
 * Des: 阿里大于短信
 */
include_once FRAMEWORK_PATH.'plugins'.SEPARATE . "ali/TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

class aliSms
{
    private $top_client;
    private $request;
    private $sms_type = "normal";
    private $sms_free_sign_name = "有得有家";
    private $sms_template_code = [
//        'login_code' => ['code' => 'SMS_13036050', 'params' => ['code', 'product']],//登录确认验证码
//        'register_code' => ['code' => 'SMS_13036048', 'params' => ['code', 'product']],//用户注册验证码
//        'cpw_code' => ['code' => 'SMS_13036046', 'params' => ['code', 'product']],//修改密码验证码
        'login_code' => 'SMS_13036050',//登录确认验证码
        'register_code' => 'SMS_13036048',//用户注册验证码
        'cpw_code' => 'SMS_13036046',//修改密码验证码
        'default_code' => 'SMS_12970983',//修改密码验证码
        'balance_apply' => 'SMS_34845585',//提现申请通知
        'balance_apply_success' => 'SMS_34825473',//提现申请通知
//        'qrcode' => 'SMS_82160033',//提现申请通知
        'qrcode' => 'SMS_118705019',//提现申请通知
    ];

    public function __construct()
    {
        $this->top_client = new TopClient;
        $this->request = new AlibabaAliqinFcSmsNumSendRequest;
        $this->setAppKey();
        $this->setAppSecret();
    }

    /**
     * 设置key
     */
    public function setAppKey()
    {
        $this->top_client->appkey = "23429224";
    }

    /**
     * 设置secret
     */
    public function setAppSecret()
    {
        $this->top_client->secretKey = "5d745e1a5f582ab214aa46c0f0face9d";
    }

    /**
     * 短信类型，传入值请填写normal
     * @param $sms_type
     */
    public function setSmsType($sms_type)
    {
        $this->request->setSmsType($sms_type);
    }

    /**
     * 短信签名
     * @param string $name
     */
    public function setSmsFreeSignName($name = '')
    {
        $this->request->setSmsFreeSignName(($name == '' ? $this->sms_free_sign_name : $name));
    }

    /**
     * 短信模板变量
     * @param $params
     */
    public function setSmsParam($params)
    {
        $this->request->setSmsParam(json_encode($params));
    }

    /**
     * 短信接收号码
     * 群发短信需传入多个号码，
     * 以英文逗号分隔，
     * 一次调用最多传入200个号码。
     * 示例：18600000000,13911111111,13322222222
     * @param $mobile
     */
    public function setRecNum($mobile)
    {
        $this->request->setRecNum($mobile);
    }

    /**
     * 短信模板ID
     * @param $code
     */
    public function setSmsTemplateCode($code)
    {
        $this->request->setSmsTemplateCode($this->sms_template_code[$code]);
    }

    /**
     * 执行
     * @return mixed|ResultSet|SimpleXMLElement
     */
    public function execute()
    {
        return $this->top_client->execute($this->request);
    }

    /**
     * 发送信息
     * @param $mobile
     * @param $params
     * @param $code
     * @return array
     */
    public function sendSms($mobile, $params, $code)
    {
        $this->setSmsType($this->sms_type);
        $this->setSmsFreeSignName($this->sms_free_sign_name);
        $this->setSmsParam($params);
        $this->setRecNum($mobile);
        $this->setSmsTemplateCode($code);
        return $this->resultAnalysis($this->execute());
    }

    /**
     * 发送结果分析
     * @param $res
     * @return array
     */
    public function resultAnalysis($res)
    {
        include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
        $log = new log();
        $return = [];
        if (isset($res->result->err_code) && $res->result->err_code == 0) {
            $return = ['status' => 1, 'code' => 1, 'msg' => 'success'];
        } elseif (isset($res->code)) {
            $return = ['status' => 0, 'code' => $res->code, 'msg' =>'failed'];
            $log->debug_write("mobile:".json_encode($res));
        }
        return $return;
    }
}
